/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epam.javatanf2013.web.examples.servlets;

import com.epam.javatanf2013.services.CustomerService;
import com.epam.javatanf2013.services.ServiceException;
import com.epam.javatanf2013.services.bo.CustomerBo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

/**
 *
 * @author Istvan_Monori
 */
public class CustomerListServlet extends HttpServlet {

    
    @Autowired
    private CustomerService customerService;

    public CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
                config.getServletContext());
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Logger log = Logger.getLogger(CustomerListServlet.class.getName());
        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Customer List</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Customer List</h1>");
            
            List<CustomerBo> list = getCustomerService().findAllCustomer();
            out.println("<table>");
            out.println("<tr>");
            out.println("<th>");
            out.println("Id");
            out.println("</th>");
            out.println("<th>");
            out.println("Customer name");
            out.println("</th>");
            out.println("<th>");
            out.println("Actions");
            out.println("</th>");
            out.println("</tr>");
            for (CustomerBo dto: list) {
                out.println("<tr>");
                out.println("<td>");
                out.println("<a href=\""+request.getContextPath()+"/Customer/Edit?id="+dto.getId()+"\">");
                out.println(dto.getId());
                out.println("</a>");
                out.println("</td>");
                out.println("<td>");
                out.println(dto.getFullName());
                out.println("</td>");
                out.println("<td>");
                out.println("<a href=\""+request.getContextPath()+"/Customer/Edit?id="+dto.getId()+"\">");
                out.println("Modify");
                out.println("</a>");
                out.println("</td>");
                out.println("</tr>");
            }
            out.println("</table>");
            out.println("<td>");
            out.println("<a href=\""+request.getContextPath()+"/Customer/Edit\">");
            out.println("New");
            out.println("</a>");
            
            
            out.println("</body>");
            out.println("</html>");
        } catch (ServiceException ex) {
            log.log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
